<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/7
 * Time: 10:21
 */

function filterUrl($param) {
    $url = $_SERVER['PHP_SELF'];
    //
    $re = "/\/$param\/[^\/]+/";
    return preg_replace($re,'',$url);
}


function removeXSS($data){
    require_once CORE_PATH . 'HtmlPurifier/HTMLPurifier.auto.php';
    $_clean_xss_config = HTMLPurifier_Config::createDefault();
    $_clean_xss_config->set('Core.Encoding', 'UTF-8');
    $_clean_xss_config->set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]');
    $_clean_xss_config->set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
    $_clean_xss_config->set('HTML.TargetBlank', TRUE);
    $_clean_xss_obj = new HTMLPurifier($_clean_xss_config);
    return $_clean_xss_obj->purify($data);

}

function showImage($url,$width = '',$height = ''){
    $ic = C('IMAGE_CONFIG');
    if ($width)
        $width = "'width'=$width";
    if ($height)
        $height = "'height'=$height";
    echo "<img $width $height src='{$ic['viewPath']}$url' />";
}

function uploadOne($imgName,$dirName,$thumb = array()) {
    if (isset($_FILES[$imgName]) && $_FILES[$imgName]['error'] == 0){
        $ic = C('IMAGE_CONFIG');
        $upload = new \Think\Upload(array(
            'rootPath' => $ic['rootPath'],
            'maxSize'  => $ic['maxSize'],
            'exts'     => $ic['exts'],
        ));
        $upload->savePath = $dirName.'/';//图片二级目录的名称
        //上传文件
        $info = $upload->upload(array($imgName => $_FILES[$imgName]));
        if (!$info){
            return array(
              'ok' => 0,
              'error' => $upload->getError(),
            );
        } else {
            $ret['ok'] = 1;
            $ret['images'][0] = $logoName = $info[$imgName]['savepath'].$info[$imgName]['savename'];

            if ($thumb){
                $image = new \Think\Image();
                foreach ($thumb as $k => $v) {
                    $ret['images'][$k+1] = $info[$imgName]['savepath'].'thumb_'.$k.'_'.$info[$imgName]['savename'];
                    $image->open($ic['rootPath'].$logoName);
                    $image->thumb($v[0],$v[1])->save($ic['rootPath'].$ret['images'][$k+1]);
                }
            }
            return $ret;
        }
    }
}

/**
 * 显示图片
 * @param  string $img
 * @param  string $dir
 * @param  string $default
 * @return string
 */
function showImg( $img, $dir, $default ){
    if( file_exists( C( 'PUBLIC_PATH' ) . $dir . $img ) ){
        return C( 'PUBLIC_URL' ) . $dir . $img;
    }else{
        if( preg_match( "/sm_*/", $img ) ){
            return C( 'PUBLIC_URL' ) . "{$dir}{$default}sm_default.jpg";
        }elseif( preg_match( "/mid_*/", $img ) ){
            return C( 'PUBLIC_URL' ) . "{$dir}{$default}mid_default.jpg";
        }elseif( preg_match( "/big_*/", $img ) ){
            return C( 'PUBLIC_URL' ) . "{$dir}{$default}big_default.jpg";
        }elseif( preg_match( "/mbig_*/", $img ) ){
            return C( 'PUBLIC_URL' ) . "{$dir}{$default}mbig_default.jpg";
        }else{
            return C( 'PUBLIC_URL' ) . "{$dir}{$default}default.jpg";
        }
    }
}

/**
 * 为一个定单生成支付宝支付按钮
 *
 * @param unknown_type $orderId
 */
function makeAlipayBtn($orderId, $btnName = '去支付宝支付')
{
    //return require('./alipay/alipayapi.php');
}

function deleteImage($image = array()){
    $savePath = C('IMAGE_CONFIG');
    foreach ($image as $v) {
        unlink($savePath['rootPath'] . $v);
    }
}

function buildSelect($tableName,$selectName,$valueFieldName,$textFieldName,$selectedValue=''){
    $model = D($tableName);
    $data = $model->field("$valueFieldName,$textFieldName")->select();
    $select = "<select name='$selectName'><option value=''>请选择</option>";
    foreach ($data as $k => $v){
        $value = $v[$valueFieldName];
        $text = $v[$textFieldName];
        if ($selectedValue && $selectedValue==$value){
            $selected = 'selected="selected"';
        } else {
            $selected = '';
        }

        $select .= '<option '.$selected.' value="'.$value.'">'.$text.'</option>';
    }
    $select .= "</select>";
    echo $select;
}